package com.smedi.ismedi.inmail.api.validator;

import com.smedi.ismedi.inmail.api.validator.AllowValueEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


public class EnumValueValidator implements ConstraintValidator<AllowValueEnum, Object> {
    private String[] strValues;
    private int[] intValues;

    @Override
    public void initialize(AllowValueEnum constraintAnnotation) {
        strValues = constraintAnnotation.strValues();
        intValues = constraintAnnotation.intValues();
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null)
            return true;
        if (value instanceof String) {
            for (String s : strValues) {
                if (s.equals(value)) {
                    return true;
                }
            }
        } else if (value instanceof Integer) {
            for (Integer s : intValues) {
                if (s == value) {
                    return true;
                }
            }
        }
        return false;
    }

}